package com.yaolaizaixian.merchant.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.bean.OrderList;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

/**
 * Created by ylzx on 2017/2/27.
 */
public class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.ViewHolder> {

    public static final int FIRST_STICKY_VIEW = 1;
    public static final int HAS_STICKY_VIEW = 2;
    public static final int NONE_STICKY_VIEW = 3;

    //1 订单列表 2 搜索页 扫码页
    private String isflag;
    public List<OrderList> datas;
    private Context mcontext;

    private OnItemClickListener mOnItemClickListener;

    public OrderListAdapter(Context context, List<OrderList> datas, String flag) {

        this.datas = datas;
        this.mcontext = context;
        this.isflag = flag;

    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_orderlist, viewGroup, false);
        return new ViewHolder(view);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

        OrderList orderList = datas.get(position);

        if (datas.get(position).isred())

        {

            viewHolder.iv_weidu.setVisibility(View.VISIBLE);

        }

        else {

            viewHolder.iv_weidu.setVisibility(View.GONE);
        }

        String time = orderList.getInput_time().substring(0, 10);
        // 商户名
        viewHolder.tvOrderlistShopname.setText(orderList.getVendorName());
        //店铺名
        viewHolder.tvOrderlistShop.setText(orderList.getStoreName());
        //商户订单号
        viewHolder.tvOrderlistNumber.setText(orderList.getVenderStreamAccount());
        //耀莱订单号
        viewHolder.tvOrderlistShophao.setText(orderList.getLife_so_master_id());
        //订单时间
        viewHolder.tvOrderlistTime.setText(orderList.getInput_time());
        //订单金额
        viewHolder.tvOrderlistMoney.setText("￥" + orderList.getTotal_amt());
        //销售员
        viewHolder.tvOrderlistName.setText(orderList.getEnterpriseEmployeeName());
        //备注
        viewHolder.tvSalesweekBeizhu.setText(orderList.getRemark());

        //客户姓名
        if (!TextUtils.isEmpty(orderList.getCustomerName())){
            viewHolder.tvOrderlistClientname.setText(orderList.getCustomerName().substring(0,1)+"先生/女士");
        }
        else{

            if (!TextUtils.isEmpty(orderList.getPhone())&&orderList.getPhone().length()==11){
                viewHolder.tvOrderlistClientname.setText(orderList.getPhone().substring(7, 11));
            }
        }
        //客户手机尾号
        if (!TextUtils.isEmpty(orderList.getPhone())&&orderList.getPhone().length()==11){
            viewHolder.tvOrderlistPhone.setText(orderList.getPhone().substring(7,11));
        }
        //状态  完成补录状态(0:需补录，1:已完成)
//        订单状态 ((5：待确定 10：确定 15：已支付 100：交易成功 -100：交易关闭))
//        待确认 5，待收款10，已收款：>=15，待补录:已收款+原来补录字段，已完成：已收款+原来补录字段，已取消：-100
        if ("5".equals(orderList.getStatus())) {
            viewHolder.tvOrderlistState.setText("待确定");
            viewHolder.tvOrderlistStateNumber.setText("待确定");
            viewHolder.tvOrderlistStateNumber.setTextColor(mcontext.getResources().getColor(R.color.text_statedaique));
            viewHolder.ivCircle.setImageResource(R.drawable.circle_stayok);

        } else if ("-100".equals(orderList.getStatus())) {
            viewHolder.tvOrderlistState.setText("已取消");
            viewHolder.tvOrderlistStateNumber.setText("已取消");
            viewHolder.tvOrderlistStateNumber.setTextColor(mcontext.getResources().getColor(R.color.text_statequxiao));
            viewHolder.ivCircle.setImageResource(R.drawable.circle_cancle);
            viewHolder.iv_weidu.setVisibility(View.GONE);

        } else if ("10".equals(orderList.getStatus())) {

            viewHolder.tvOrderlistState.setText("待收款");
            viewHolder.tvOrderlistStateNumber.setText("待收款");
            viewHolder.tvOrderlistStateNumber.setTextColor(mcontext.getResources().getColor(R.color.text_statedaipay));
            viewHolder.ivCircle.setImageResource(R.drawable.circle_staypay);

        } else if (Integer.parseInt(orderList.getStatus())>=15) {

            if ("0".equals(orderList.getIsDitional())) {
                viewHolder.tvOrderlistState.setText("需补录");
                viewHolder.tvOrderlistStateNumber.setText("需补录");
                viewHolder.tvOrderlistStateNumber.setTextColor(mcontext.getResources().getColor(R.color.text_statebulu));
                viewHolder.ivCircle.setImageResource(R.drawable.circle_bulu);
            } else if ("1".equals(orderList.getIsDitional())) {

                viewHolder.tvOrderlistState.setText("已完成");
                viewHolder.tvOrderlistStateNumber.setText("已完成");
                viewHolder.tvOrderlistStateNumber.setTextColor(mcontext.getResources().getColor(R.color.text_statesuccess));
                viewHolder.ivCircle.setImageResource(R.drawable.circle_sucess);
                viewHolder.iv_weidu.setVisibility(View.GONE);

            }

        }

        if (TextUtils.isEmpty(orderList.getPay_type_sysno())){

            viewHolder.tvOrderlistZhitype.setText("");

        }else {

            if ("168".equals(orderList.getPay_type_sysno())){

                viewHolder.tvOrderlistZhitype.setText("微信支付");
                viewHolder.tvOrderlistZhitype.setTextColor(mcontext.getResources().getColor(R.color.red));
            }
            //之前的订单，全部归结为线下
            else {

                viewHolder.tvOrderlistZhitype.setText("线下支付");
                viewHolder.tvOrderlistZhitype.setTextColor(mcontext.getResources().getColor(R.color.red));
            }
        }

        //悬浮
        if (position == 0) {

                viewHolder.tvTime.setVisibility(View.VISIBLE);
                viewHolder.tvTime.setText(orderList.getInput_time().substring(0, 10));
                viewHolder.itemView.setTag(FIRST_STICKY_VIEW);

        }
        else {

            if (!TextUtils.equals(orderList.getInput_time().substring(0, 10), datas.get(position - 1).getInput_time().substring(0, 10))) {

                viewHolder.tvTime.setVisibility(View.VISIBLE);
                viewHolder.tvTime.setText(orderList.getInput_time().substring(0, 10));
                viewHolder.itemView.setTag(HAS_STICKY_VIEW);

            } else {

                viewHolder.tvTime.setVisibility(View.GONE);
                viewHolder.itemView.setTag(NONE_STICKY_VIEW);
            }
        }

        viewHolder.itemView.setContentDescription(orderList.getInput_time().substring(0, 10));

        if (mOnItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    mOnItemClickListener.onClick(position);
                }
            });

            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    mOnItemClickListener.onLongClick(position);
                    return false;
                }
            });
        }
    }

    //获取数据的数量
    @Override
    public int getItemCount() {

        return datas.size();

    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTime;
        AutoLinearLayout llItemContent;
        TextView tvOrderlistShopname;
        ImageView iv_weidu;
        ImageView ivCircle;
        TextView tvOrderlistStateNumber;
        LinearLayout dashLine02;
        TextView tvOrderlistNumber;
        TextView tvOrderlistShop;
        TextView tvOrderlistTime;
        TextView tvOrderlistMoney;
        TextView tvOrderlistZhitype;
        TextView tvOrderlistState;
        TextView tvOrderlistShophao;
        TextView tvOrderlistName;
        TextView tvSalesweekBeizhu;
        TextView tvOrderlistClientname;
        TextView tvOrderlistPhone;

        public ViewHolder(View view) {
            super(view);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            llItemContent = (AutoLinearLayout) view.findViewById(R.id.ll_item_content);
            tvOrderlistShopname = (TextView) view.findViewById(R.id.tv_orderlist_shopname);
            ivCircle = (ImageView) view.findViewById(R.id.iv_circle);
            tvOrderlistStateNumber = (TextView) view.findViewById(R.id.tv_orderlist_statenumber);
            dashLine02 = (LinearLayout) view.findViewById(R.id.dashLine02);
            tvOrderlistNumber = (TextView) view.findViewById(R.id.tv_orderlist_number);
            tvOrderlistShop = (TextView) view.findViewById(R.id.tv_orderlist_shop);
            tvOrderlistTime = (TextView) view.findViewById(R.id.tv_orderlist_time);
            tvOrderlistMoney = (TextView) view.findViewById(R.id.tv_orderlist_money);
            tvOrderlistZhitype = (TextView) view.findViewById(R.id.tv_orderlist_zhitype);
            tvOrderlistState = (TextView) view.findViewById(R.id.tv_orderlist_state);
            tvOrderlistShophao = (TextView) view.findViewById(R.id.tv_orderlist_shophao);
            tvOrderlistName = (TextView) view.findViewById(R.id.tv_orderlist_name);
            tvSalesweekBeizhu = (TextView) view.findViewById(R.id.tv_salesweek_beizhu);
            tvOrderlistClientname = (TextView)view.findViewById(R.id.tv_orderlist_clientname);
            tvOrderlistPhone = (TextView)view.findViewById(R.id.tv_orderlist_phone);
            iv_weidu = (ImageView) view.findViewById(R.id.iv_weidu);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
